package xyz.jangle.action;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import xyz.jangle.model.User;
import xyz.jangle.service.UserService;


/**
 * @author huhj
 * @email  jangle@jangle.xyz
 * @time   2018年7月6日 上午10:27:20
 */
@Controller
@RequestMapping("/hello")
public class HelloAjax {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/helloAjax")
	public ModelAndView hello(){
		ModelAndView mav = new ModelAndView("helloAjax"); 	//参数为试图名称 viewName
		return mav;
	}
	
	@RequestMapping("/helloData")
	@ResponseBody
	public Map<String, Object> helloData(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<User> users = userService.getAllUser();
		map.put("users",users);
		return map;
	}
	
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> test(@RequestBody String test){
		Map<String,Object> map = new HashMap<String,Object>();
		//string
		System.out.println("test:"+test);
		try {
//			test = URLDecoder.decode(test);
			test = URLDecoder.decode(test,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
//		System.out.println(URLDecoder.decode(test));
		String[] split = test.split("&");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);
			String[] split2 = split[i].split("=");
			map.put(split2[0], split2[1]);
		}
		// 如果是验证地址的请求，则返回challenge。
		if(map.get("challenge") !=null) {
			Map<String,Object> map2 = new HashMap<String,Object>();
			map2.put("challenge", map.get("challenge"));
			return map2;
		}
		
		// 解析消息内容
//		System.out.println(map.get("event%5Bmessage%5D%5Bcontent%5D"));
		System.out.println(map.get("event[message][content]"));
		JSONObject jsonObject = JSONObject.parseObject((String) map.get("event[message][content]"));
		String text = jsonObject.getString("text");
		System.out.println(text);
		
		
		
//		System.out.println(test.getSchema());
//		System.out.println(test.getHeader().getEvent_id());
//		System.out.println(test.getHeader().getEvent_id());
//		System.out.println(test.getHeader().get("event_id"));
//		System.out.println(test.getEvent().get("message"));
//		System.out.println(test.getEvent().get("event").get("message"));
//		map.put("test", test.getSchema());
		return map;
	}
	
	
	@RequestMapping(value = "/test2", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> test2(@RequestBody String test){
		Map<String,Object> map = new HashMap<String,Object>();
		//string
		System.out.println("test:"+test);
		JSONObject jsonObject2 = JSONObject.parseObject(test);
		if(jsonObject2.getString("challenge")!=null) {
			map.put("challenge", jsonObject2.getString("challenge"));
			return map;
		}
		String event = jsonObject2.getString("event");
//		System.out.println(event);
		JSONObject eventObject = JSONObject.parseObject(event);
		String message = eventObject.getString("message");
//		System.out.println(message);
		JSONObject messageObject = JSONObject.parseObject(message);
		String content = messageObject.getString("content");
//		System.out.println(content);
		JSONObject contentObject = JSONObject.parseObject(content);
		String text = contentObject.getString("text");
		System.out.println(text);
		if(text == null) {
			return map;
		}
		
		
		return map;
	}

}
